<?php

	header("Content-Type: text/html;charset=utf-8");

	include("../function/_nosql.php"); //
	include("../function/function.php");
	include("../_include.user.judge.php");

	$WIDout_trade_no = chkstr(trim($_REQUEST['WIDout_trade_no']));

	$aid = chkstr(trim($_REQUEST['aid']));
	$uid = $_SESSION['kboy_userid'];
	$time = date("Y");
	$order_num = getClassName("kboy_order","order_num","order_no='".$WIDout_trade_no."'");
	$moneys = getClassName("kboy_forum_active","active_all_money","id=$aid");
	$active_tian = getClassName("kboy_forum_active","active_tian","id=".$aid);
	if(strstr($WIDout_trade_no,'_')){//申请成为cva

	     mysql_query("update  kboy_user  set user_trade_no='".$WIDout_trade_no."' where uid= '".$_SESSION['kboy_userid']."' ");

		 $money = 50;

		 $subject ='申请成为CVA';
	}

	else{//提交订单

	    if(empty($aid)){

			echo erro("参数错误");

		}

		if(!is_numeric($aid)){

			echo erro("参数错误");

		}

		if(getNewsNums("kboy_forum_active","id=$aid") <1){

			echo erro("参数错误");

		}

		$order_num = getClassName("kboy_order","order_num","order_no='".$WIDout_trade_no."'");

		if(getClassName("kboy_order","order_isfull","order_no='".$WIDout_trade_no."'")  == 1 ){

			if(getClassName("kboy_user","user_style"," uid= '".$_SESSION['kboy_userid']."' ")  == 1){//为cva会员

				if(getClassName("kboy_jiangli","hdnian","uid=$uid and hd_jieshu=0") == $time and getClassName("kboy_jiangli","hd_yhui","uid=$uid and hd_jieshu=0") == 1){
					$leiji = getClassName("kboy_jiangli","hd_leiji","uid=$uid and hd_jieshu=0");
					$renshu = getClassName("kboy_jiangli","hdrenshu","uid=$uid and hd_jieshu=0");
					if($order_num == 1){
						if($active_tian < 3 and $renshu > 30){
						$money = ($moneys*0.90);
						$jl_shuom = "带入新会员".$renshu."人，优惠10%";
						}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
							$money = ($moneys*0.95);
							$jl_shuom = "累计参加".$leiji."次，优惠5%";
						}else if($active_tian < 3 and $leiji > 30){
							$money = ($moneys*0.90);
							$jl_shuom = "累计参加".$leiji."次，优惠10%";
						}
					}else if($order_num == 2 or $order_num == 3){
						if($active_tian < 3 and $renshu > 30){
							$benren = ($moneys*0.90);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "带入新会员".$renshu."人，和".($order_num-1)."位朋友，优惠10%";
						}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
							$benren = ($moneys*0.95);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠5%";
						}else if($active_tian < 3 and $leiji > 30){
							$benren = ($moneys*0.9);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠10%";
						}
					}else if($active_tian < 3 and $order_num-1 < 6 and  $order_num-1 > 2){
						$benren = ($moneys*0.70);
						$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠30%";
					}else if($active_tian < 3 and $order_num-1 > 5){
						$benren = ($moneys*0.50);
						$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠50%";
					}
						if(isset($jl_shuom)){
							$jl_danhao= getClassName('kboy_jljilu','jl_danhao',"uid='$uid' and aid = '$aid'");
							if($jl_danhao){
								mysql_query("update kboy_jljilu jl_danhao='$WIDout_trade_no' where uid='$uid' and aid = '$aid'");
							}else{
								$mysql = new sqlstr("kboy_jljilu");
								$mysql -> set("uid",$uid);
								$mysql -> set("aid",$aid);
								$mysql -> set("jl_shuom",$jl_shuom);
								$mysql -> set("jl_danhao",$WIDout_trade_no);
								$mysql -> set("jl_jine",$money);
								$mysql -> set("jl_time",date("Y-m-d H:i:s", time()));
								$news_sql = $mysql -> insertSql();
								$news_res = mysql_query($news_sql);
							}
						}else{
							$moneys = getClassName("kboy_forum_active","active_cva_money","id=$aid");
							$money = ($moneys*$order_num);
						}
					
									
									
					}else{
						$moneys = getClassName("kboy_forum_active","active_cva_money","id=$aid");
						$money = ($moneys*$order_num);
					}

			}

			else{

				$moneys = getClassName("kboy_forum_active","active_all_money","id=$aid");
				$money = ($moneys*$order_num);
			}

		 

		}

		else{

		 $moneys = getClassName("kboy_forum_active","active_part_money","id=$aid");
		$money = ($moneys*$order_num);
		}
		$subject = getClassName("kboy_forum_active","active_title","id=$aid");
	}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>-</title>

</head>

<?php

/* *

 * 功能：即时到账交易接口接入页

 * 版本：3.3

 * 修改日期：2012-07-23

 * 说明：

 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。

 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。



 *************************注意*************************

 * 如果您在接口集成过程中遇到问题，可以按照下面的途径来解决

 * 1、商户服务中心（https://b.alipay.com/support/helperApply.htm?action=consultationApply），提交申请集成协助，我们会有专业的技术工程师主动联系您协助解决

 * 2、商户帮助中心（http://help.alipay.com/support/232511-16307/0-16307.htm?sh=Y&info_type=9）

 * 3、支付宝论坛（http://club.alipay.com/read-htm-tid-8681712.html）

 * 如果不想使用扩展功能请把扩展功能参数赋空值。

 */



require_once("alipay.config.php");

require_once("lib/alipay_submit.class.php");



/**************************请求参数**************************/



        //支付类型

        $payment_type = "1";

        //必填，不能修改

        //服务器异步通知页面路径

        $notify_url = $web_url."/alipay/notify_url.php";

        //需http://格式的完整路径，不能加?id=123这类自定义参数



        //页面跳转同步通知页面路径

        $return_url = $web_url."/alipay/return_url.php";

        //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/



        //卖家支付宝帐户

        $seller_email = "ivv2009@163.com";

        //必填



        //商户订单号

        $out_trade_no = $WIDout_trade_no;

        //商户网站订单系统中唯一订单号，必填



        //订单名称

        $subject = $subject;

        //必填



        //付款金额
        if($money==0){
        	echo erro('为0不支付');
        }
        
        $total_fee = $money;

        //必填



        //订单描述



        $body = "收费";

        //商品展示地址

        $show_url = "";

        //需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html



        //防钓鱼时间戳

        $anti_phishing_key = "";

        //若要使用请调用类文件submit中的query_timestamp函数



        //客户端的IP地址

        $exter_invoke_ip = "";

        //非局域网的外网IP地址，如：221.0.0.1





/************************************************************/



//构造要请求的参数数组，无需改动

$parameter = array(

		"service" => "create_direct_pay_by_user",

		"partner" => trim($alipay_config['partner']),

		"payment_type"	=> $payment_type,

		"notify_url"	=> $notify_url,

		"return_url"	=> $return_url,

		"seller_email"	=> $seller_email,

		"out_trade_no"	=> $out_trade_no,

		"subject"	=> $subject,

		"total_fee"	=> $total_fee,

		"body"	=> $body,

		"show_url"	=> $show_url,

		"anti_phishing_key"	=> $anti_phishing_key,

		"exter_invoke_ip"	=> $exter_invoke_ip,

		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))

);



//建立请求

$alipaySubmit = new AlipaySubmit($alipay_config);

$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");

echo "<div style=\"display:none\">".$html_text."</div>";



?>

</body>

</html>